package com.dsh.search.thread;

import java.util.concurrent.*;

public class ThreadTest {
    // 当前线程池只有一两个
    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main ..... start ...");

//        CompletableFuture.runAsync(()->{
//            System.out.println("当前线程"+Thread.currentThread().getId());
//            int i = 10/2;
//            System.out.println(i);
//        },executor);

        /**
         * executor 感知结果
         */
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程" + Thread.currentThread().getId());
//            int i = 10 / 0;
//            System.out.println(i);
//            return i;
//        }, executor).whenComplete((res,excption)->{
//            System.out.println("异步任务完成了 ...  结果是" + res +" 异常是" +excption);
//        }).exceptionally(throwable -> {
//            return 10;
//        });


        /**
         *  handle  方法执行完成后的处理
         */
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程" + Thread.currentThread().getId());
//            int i = 10 / 0;
//            System.out.println(i);
//            return i;
//            // 感知结果
//        }, executor).handle((res, thr) -> {
//            if (res != null) {
//                return res * 2;
//            }
//            if (thr != null) {
//                return 0;
//            }
//            return  0;
//        });

        /**
         * 串行化不能获取到上一步的执行结果 无返回值
         */
//        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果"+i);
//            return i;
//            //executor 感知结果  handle  方法执行完成后的处理
//        }, executor).thenApplyAsync(res->{
//            System.out.println("任务 2 启动了 " +res );
//            return "Hello" + res;
//            },executor);

//        Integer integer = future.get();
        /**
         * 两个都完成
//         */
//        CompletableFuture<Object> future01 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务 1 线程" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("任务 1 结束" );
//            return i;
//        }, executor);
//        CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务 2 线程" + Thread.currentThread().getId());
//            try {
//                Thread.sleep(3000);
//                System.out.println("任务 2 结束" );
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//
//            return " Hello ";
//        }, executor);

        /**
         *  无法感知结果
         */
//        future01.runAfterBothAsync(future02,()->{
//            System.out.println("任务 3  启动 ");
//        },executor);
        /**
         * 任务3 感知结果
         */
//        future01.thenAcceptBothAsync(future02,(f1,f2)->{
//            System.out.println("任务3 开始 .. 之前的结果 " +f1 + ">>" +f2);
//        },executor);


//        CompletableFuture<String> future = future01.thenCombineAsync(future02, (f1, f2) -> {
//            return f1 + " : " + f2 + " -> Hello";
//        }, executor);

        /**
         * 只要有一个执行任务 3
         */
//        future01.runAfterEitherAsync(future02,()->{
//            System.out.println("任务 3  启动 ");
//        },executor);
//        future01.acceptEitherAsync(future02,(res)->{
//            System.out.println("任务 3  启动 ");
//        },executor);

//        CompletableFuture<String> future = future01.applyToEitherAsync(future02, (res) -> {
//            System.out.println("任务3开始之前的结果 " + res);
//            return res.toString() + "->  对对";
//        }, executor);

        /*
        等待全部成功
         */
        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询图片的信息");
            return "hello.jpg";
        },executor);

        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(7000);
                System.out.println("查询图片的属性");

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "黑丝 + 256 ";
        },executor);

        CompletableFuture<String> futureDesc= CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
                System.out.println("查询图片的介绍");

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "华为";
        }, executor);

//        futureAttr.get();
//        futureDesc.get();
//        futureImg.get();

//        CompletableFuture<Void> allOr = CompletableFuture.allOf(futureAttr, futureDesc, futureImg);
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureAttr, futureDesc, futureImg);


//        allOr.get();// 等待所以结果完成
        anyOf.get();// 等待所以结果完成
        System.out.println("main ..... end ...");
//        System.out.println("main ..... end ..." + "->" + futureAttr.get() + "->" + futureDesc.get()+"->" + futureImg.get());

//        executor.shutdown();
    }


    public void thread(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main ..... start ...");
//        Thread thread = new Thread01();
//        thread.start();

//        Runable01 runable01 = new Runable01();
//        new Thread(runable01).start();
        //堵塞等待整个线程完成
//        FutureTask<Integer> futureTask = new FutureTask<>(new Callable01());
//        new Thread(futureTask).start();
//        Integer integer = futureTask.get();
//        System.out.println("integer = " + integer);


        executor.execute(new Runable01());

//        ThreadPoolExecutor executor = new ThreadPoolExecutor()


        System.out.println("main ..... end ...");

    }


    //相当于后台运行
    public static class Thread01 extends Thread {
        @Override
        public void run() {
            System.out.println("当前线程" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println(i);
        }
    }

    public static class Runable01 implements Runnable {
        @Override
        public void run() {
            System.out.println("当前线程" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println(i);
        }
    }

    public static class Callable01 implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println(i);
            return i;
        }
    }
}
